package hotciv.common;

import hotciv.framework.*;

public class UnitImpl implements Unit
{
	private String unitType;
	private Player player;
	private int moveCount = 0;
	private int defenceStrength = 0;
	private int attackStrength = 0;
	
	public UnitImpl(String unitType, Player player)
	{
		this.unitType = unitType;
		this.player = player;
		
		if(unitType.equals(GameConstants.ARCHER)) 
		{
			moveCount = GameConstants.ARCHERMOVE;
			defenceStrength = GameConstants.ARCHERDEF;
			attackStrength = GameConstants.ARCHERATT;
		}
		else if (unitType.equals(GameConstants.LEGION))
		{
			moveCount = GameConstants.LEGIONMOVE;
			defenceStrength = GameConstants.LEGIONDEF;
			attackStrength = GameConstants.LEGIONATT;
		}
		else if (unitType.equals(GameConstants.SETTLER))
		{
			moveCount = GameConstants.SETTLERMOVE;
			defenceStrength = GameConstants.SETTLERDEF;
			attackStrength = GameConstants.SETTLERATT;
		} else if (unitType.equals(GameConstants.FORTIFIED_ARCHER)) {
			this.unitType = GameConstants.ARCHER;
			moveCount = 0;
			defenceStrength = GameConstants.ARCHERDEF * 2;
			attackStrength = GameConstants.ARCHERATT;
		}
		
	}
	
	
	public String getTypeString()
	{
		return unitType;
	}
	public int getMoveCount()
	{
		return moveCount;
	}

	public Player getOwner()
	{
		return player;
	}
	@Override
	public int getDefensiveStrength() 
	{
		return defenceStrength;
	}
	@Override
	public int getAttackingStrength() 
	{
		return attackStrength;
	}
	
	public boolean equals(Object o) {
	    if (this == o) return true;
		if (o == null) { return false; }
	    if (o.getClass() != this.getClass()) { return false; }
	    Unit other = (Unit) o;
	    return getOwner().equals(other.getOwner()) && getTypeString().equals(other.getTypeString())
	    		&& getMoveCount() == other.getMoveCount() && getDefensiveStrength() == other.getDefensiveStrength() && getAttackingStrength() == other.getAttackingStrength();
	  }
	
	public String toString() {
		return getOwner().name()+":"+getTypeString();
	}
	
	public int hashCode() {
	    // works ok for positions up to columns == 479
	    return 511*player.hashCode()+unitType.hashCode()+moveCount+attackStrength+defenceStrength;
	}
}